package com.fare.taluo.web.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.flight.AirlineApplication;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.core.AirlineEntityVO;
import com.fare.taluo.vo.core.FdcLineEntityVO;
import com.fare.taluo.vo.core.SourceEntityVO;

@Controller("airlineAction")
@Scope("prototype")
public class AirlineAction extends BaseFdcAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7340850821930621177L;
	@Resource(name = "airlineApplication")
	private AirlineApplication airlineApplication;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	@Resource(name="portApplication")
	private PortApplication portApplication;
	private List<AirlineEntityVO> list = new ArrayList<AirlineEntityVO>();
	private  List<SourceEntityVO>  sourceLists;
	private String companyCode;
	private String fromCityName;
	private String toCityName;
	/*
	 * public void getData() {
	 * 
	 * }
	 */
    
	
	private AirlineEntityVO vo;
	// private Integer page;
	// private Integer pageSize;

	private String flag = "normal";

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	
    
	public String toAnalyseJSP() {
		sourceLists = sourceApplication.findSourceEntityListByConditions("","","","",1,1000);
		returnPageURL = "/system/maintain/airline.jsp";
		return "dispatcher";
	}
	public void getData() throws JsonGenerationException, JsonMappingException, IOException{
		
		String fromCityCode = null;
		String toCityCode = null;
		Date time = null;
		String flightNum = null;
		if(StringUtils.isNotEmpty(fromCityName)){
			fromCityCode = portApplication.findPortEntityByCityName(fromCityName).getPortCode();
		}
		if(StringUtils.isNotEmpty(toCityName)){
			 toCityCode = portApplication.findPortEntityByCityName(toCityName).getPortCode();
		}
		List<FdcLineEntityVO> voList = null;
		try {
			  voList = airlineApplication.findAllByView(companyCode, fromCityCode, toCityCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		 String data = this.toJSON(voList, voList.size());
	     super.toJSONOut(data);
	}
	
	public String toJSON(List list,int total) throws JsonGenerationException, JsonMappingException, IOException{
		String jsonStr = "{\"Total\":"+total+","+
						 "\"Rows\":";
		int i = 0;
		String json = com.fare.taluo.util.JsonUtil.toJson(list);
		jsonStr += json;
		jsonStr += "}";
		return jsonStr;
	}
	/*public void getData() throws Exception {
		list.addAll(getAirlineApplication().findAll());

		// 拼装json数据
		StringBuffer resultBuffer = new StringBuffer();
		resultBuffer.append("{'Total':" + list.size() + "," + "'Rows':[");
		for (AirlineEntityVO airlineEntityVO : list) {
			resultBuffer.append("{'id':'" + airlineEntityVO.getId() + "',");
			resultBuffer.append("'company':'" + airlineEntityVO.getCompany()
					+ "',");
			resultBuffer.append("'source':'" + airlineEntityVO.getSource()
					+ "',");
			resultBuffer.append("'dest':'" + airlineEntityVO.getDest() + "',");
			resultBuffer.append("'voyage':'" + airlineEntityVO.getVoyage()
					+ "',");
			resultBuffer.append("'fightNo':'" + airlineEntityVO.getFightNo()
					+ "',");
			resultBuffer.append("'departTime':'"
					+ airlineEntityVO.getDepartTime() + "'");
			resultBuffer.append("},");
		}
		if (resultBuffer.toString().endsWith(","))// 去掉最后一个元素的逗号
			resultBuffer.deleteCharAt(resultBuffer.lastIndexOf(","));
		resultBuffer.append("]}");
		String json = resultBuffer.toString();
		JsonUtil.toJSONOut(json);
	}
*/
	public AirlineApplication getAirlineApplication() {
		return airlineApplication;
	}

	public void setAirlineApplication(AirlineApplication airlineApplication) {
		this.airlineApplication = airlineApplication;
	}

	public AirlineEntityVO getVo() {
		return vo;
	}

	public void setVo(AirlineEntityVO vo) {
		this.vo = vo;
	}

	public void doUpdate() { //
		HttpServletRequest request = ServletActionContext.getRequest(); //
		System.out.println("from jsp:" + request.getParameter("vo.departTime")); //
		System.out.println("from vo:" + vo.getDepartTime());
		getAirlineApplication().update(vo);

	}

	public String doSave() throws Exception {

		if (flag.equals("update")) {
			doUpdate();
		} else if (flag.equals("delete")) {
			doRemove();
		} else if (flag.equals("add")) {
			doAdd();
		}
		returnPageURL = "/system/maintain/airline.jsp";

		return "dispatcher";
	}

	public void doAdd() {

		getAirlineApplication().save(vo);

	}

	public void doRemove() {

		getAirlineApplication().remove(vo.getId());

	}

	public List<AirlineEntityVO> getList() {
		return list;
	}

	public void setList(List<AirlineEntityVO> list) {
		this.list = list;
	}
     
	public List<SourceEntityVO> getSourceLists() {
		return sourceLists;
	}

	public void setSourceLists(List<SourceEntityVO> sourceLists) {
		this.sourceLists = sourceLists;
	}
    
	public String getCompanyCode() {
		return companyCode;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}

	public String getFromCityName() {
		return fromCityName;
	}

	public void setFromCityName(String fromCityName) {
		this.fromCityName = fromCityName;
	}

	public String getToCityName() {
		return toCityName;
	}

	public void setToCityName(String toCityName) {
		this.toCityName = toCityName;
	}

}
